// pages/income-code/income-code.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    money: 0,
    money_input: 0,
    qrcode: null,
    mch_id: 0,
    show_price: false,
    mch: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().page.onLoad(this, options);
    if (options.mch_id) {
      this.setData({
        mch_id: options.mch_id
      });
    }
    this.getData();
  },

  getData: function () {
    getApp().core.showLoading({
      title: "正在生成",
      mask: !0
    });
    var self = this;
    getApp().request({
      url: getApp().api.scan_code_pay.get_qrcode,
      data: {
        price: this.data.money,
        mch_id: this.data.mch_id
      },
      success: function (e) {
        if (e.code == 0) {
          self.setData({
            qrcode: e.data.url,
            mch: e.data.mch,
            show: true
          });
        } else {
          wx.showModal({
            title: '提示',
            content: e.msg,
            success: function (result) {
              if (result.confirm) {
                wx.redirectTo({
                  delta: 1
                })
              }
            }
          });
        }
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },

  setMoney: function () {
    this.setData({
      show_price: true
    });
  },

  onClickConfirm: function () {
    this.setData({
      money: this.data.money_input,
      show_price: false,
    });
    if (this.data.money > 0) {
      this.getData();
    }
  },

  onSaveImg: function () {
    var self = this;
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success() {
        self.saveImg();
      },
      fail() {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '保存图片需要先授权保存到相册权限',
          success() {
            wx.getSetting({
              success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                  wx.openSetting({
                    success(res) {
                      if (!res.authSetting['scope.writePhotosAlbum']) {
                        wx.showToast({
                          title: '授权失败',
                          icon: 'none',
                        })
                      }
                    }
                  })
                }
              }
            });
          }
        });
      }
    });
  },

  saveImg: function () {
    getApp().core.showLoading({
      title: "下载中...",
      mask: !0
    });
    wx.downloadFile({
      url: this.data.qrcode,
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
            })
          },
          fail: function () {
            wx.showToast({
              title: '保存二维码失败',
              icon: 'none',
            })
          }
        })
      },
      fail: function () {
        wx.showToast({
          title: '下载二维码失败',
          icon: 'none',
        })
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onInput: function (e) {
    var money = e.detail.value;
    this.setData({
      money_input: money
    });
  },
})